package com.ruoyi.biz.letter.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.letter.util.CategoryUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.letter.domain.BizLetterClaim;
import com.ruoyi.biz.letter.service.IBizLetterClaimService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 信访认领Controller
 *
 * @author Fei.Cong
 * @date 2024-07-22
 */
@RestController
@RequestMapping("/letter/claim")
public class BizLetterClaimController extends BaseController {

    @Autowired
    private IBizLetterClaimService bizLetterClaimService;

    /**
     * 查询信访认领列表
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:list')")
    @GetMapping("/list")
    public AjaxResult list(BizLetterClaim bizLetterClaim) {
        LambdaQueryWrapper<BizLetterClaim> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BizLetterClaim::getLetterId, bizLetterClaim.getLetterId());
        List<BizLetterClaim> list = bizLetterClaimService.list(lambdaQueryWrapper);
        for (BizLetterClaim letterClaim : list) {
            letterClaim.setFirstCategoryName(CategoryUtil.getCacheCategory(letterClaim.getPCategoryId()));
            letterClaim.setSecondCategoryName(CategoryUtil.getCacheCategory(letterClaim.getSCategoryId()));
        }
        return success(list);
    }

    /**
     * 分页查询信访认领列表
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizLetterClaim bizLetterClaim) {
        return getDataTable(bizLetterClaimService.pageList(bizLetterClaim, startPage()));
    }

    /**
     * 导出信访认领列表
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:export')")
    @Log(title = "信访认领", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizLetterClaim bizLetterClaim) {
        List<BizLetterClaim> list = bizLetterClaimService.queryList(bizLetterClaim);
        ExcelUtil<BizLetterClaim> util = new ExcelUtil<BizLetterClaim>(BizLetterClaim.class);
        util.exportExcel(response, list, "信访认领数据");
    }

    /**
     * 获取信访认领详细信息
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizLetterClaimService.getById(id));
    }

    /**
     * 新增信访认领
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:add')")
    @Log(title = "信访认领", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizLetterClaim bizLetterClaim) {
        return toAjax(bizLetterClaimService.save(bizLetterClaim));
    }

    /**
     * 修改信访认领
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:edit')")
    @Log(title = "信访认领", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizLetterClaim bizLetterClaim) {
        return toAjax(bizLetterClaimService.updateById(bizLetterClaim));
    }

    /**
     * 删除信访认领
     */
    @PreAuthorize("@ss.hasPermi('letter:claim:remove')")
    @Log(title = "信访认领", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizLetterClaimService.removeBatchByIds(ids));
    }
}
